<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>编辑模块</title>
	<link rel="stylesheet" href="../../res/layui/css/layui.css">
	<script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
	<script type="text/javascript" src="../../res/layui/layui.js" charset="utf-8"></script>
</head>
<body style="padding:20px 40px 0px 40px;">
	<form class="layui-form" id="myForm" method="post" lay-filter="from">
		<input type="hidden" name="mmId" id="mmId" />
		<div class="layui-form-item">
			<label class="layui-form-label">上級模块</label>
			<div class="layui-input-block">
				<input type="text" id="lisMmId" lay-filter="lisMmId" placeholder="不选为根模块" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">模块名称</label>
			<div class="layui-input-inline">
				<input type="text" id="mmName" name="mmName" required
					lay-verify="required|mmName" placeholder="模块名称" autocomplete="off"
					class="layui-input">
			</div>
			<div class="layui-form-mid layui-word-aux"></div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">模块编码</label>
			<div class="layui-input-inline">
				<input type="text" id="mmEncoded" name="mmEncoded" required
					lay-verify="mmEncoded" placeholder="模块编码" autocomplete="off"
					class="layui-input">
			</div>
			<div class="layui-form-mid layui-word-aux"></div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">URL地址</label>
			<div class="layui-input-block">
				<input type="text" id="mmUrl" name="mmUrl" lay-verify="url"
					placeholder="网址，若要设置为一级模块可以不填" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item layui-form-text">
			<label class="layui-form-label">模块说明</label>
			<div class="layui-input-block">
				<textarea id="mmExplain" name="mmExplain" placeholder="请输入内容" class="layui-textarea"></textarea>
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<input type="button" class="layui-btn" lay-submit="" lay-filter="demo" value="确定">
			</div>
		</div>
		<input type="hidden" name="oper" id="oper" />
	</form>
	
<script type="text/javascript">
layui.config({
    base: "../../res/"
}).extend({
    treeSelect: "layui-xtree/treeSelect"
});

var list;
var lisMmId;

$(function(){
	var mmId = GetQueryString("mmId");
	if(mmId!='undefined'){
		findById(mmId);
	}else{
		list='';
	}
	var lismmId = GetQueryString("lismmId");
	showSelect(lismmId, mmId, list);
});

function showSelect(lismmId, mmId, list){
	//加载树形选项
	layui.use(['treeSelect','layer','form'], function () {
		var treeSelect = layui.treeSelect
		,layer = layui.layer
		,form = layui.form;
		
		lisMmId=lismmId;
		layer.msg("数据加载中.....", {
			icon: 16,
			shade: [0.5, '#f5f5f5'],
			scrollbar: false,
			offset: 'auto',
			area:['70px','70px'],
			time: 6000 //1秒关闭（如果不配置，默认是3秒）
		}, function(){
			treeSelect.checkNode('lisMmId',lismmId);
		});
		
		treeSelect.render(
        {
            elem: "#lisMmId", 
            data: "../../listenModuleManage/selectGroup.action",
            type: "post",
            // 占位符
            placeholder: '不选为根模块',
        	 // 是否开启搜索功能：true/false，默认false
            search: true,
            // 一些可定制的样式
            style: {
                folder: {
                    enable: true
                },
                line: {
                    enable: true
                }
            },
            click: function(d){
            	//点击选中的模块id
            	lisMmId = d.current.id;
            }
        });
    });
}
//表单验证
layui.use('form', function() {
	var form = layui.form;
	form.render('select', 'from');
	//自定义验证规则
	form.verify({
		mmEncoded : function(value){
			if(value.indexOf(" ")>-1){
				return "模块编码不能包含空格";
			}
		},
		mmName : function(value){
			if(value.indexOf(" ")>-1){
				return "模块名称不能包含空格";
			}else if(value.length<=0 || value.length>=20){
				return "模块名称在1~10之间";
			}
		},
		url : function(value){
			if(value.indexOf(" ")>-1){
				return "模块地址不能包含空格";
			}
		},
	});
	
	//表单监听提交
	form.on('submit(demo)', function(data){
		var name = $("#mmName").val();
		var encoded = $("#mmEncoded").val();
		var b = true;
		if(name.indexOf(" ")>-1 || (name.length<=0 || name.length>=20)){
			b=false;
		}else if(encoded.indexOf(" ")>-1){
			b=false;
		}else{
			layer.confirm('确认提交吗?', function(index){
		    	layer.close(index);
				var data = $.param({"lismmId":lisMmId}) + "&" +$("#myForm").serialize();
				var url = "../../listenModuleManage/insertOrUpdate.action";
		        $.post(url,data,function(mes){
		        	//提示框(是否增加成功或者修改成功)
					layer.msg(mes.msg, {
						  icon: mes.datas,
						  time: 1000 //1秒关闭（如果不配置，默认是3秒）
						}, function(){
						  parent.location.reload();
					});
		        },'json');
			});
			return b;
		}
	});
	
 });
	
	//得到父窗口传过来的参数
	function GetQueryString(name){
	     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	     var r = window.location.search.substr(1).match(reg);
	     if(r!=null)return  unescape(r[2]); return null;
	}
		
	//根据模块id查询所有
	function findById(id){
		var data = {"mmId":id};
		var url = "../../listenModuleManage/findById.action";
		if(id!=null && id!=""){
			$.ajax({
				type : "POST",
				url : url, 
				data : data, 
				async : false,
				success : function(mes){
					$("#mmId").val(mes.mmId);
					$("#mmName").val(mes.mmName);
					$("#mmExplain").val(mes.mmExplain);
					$("#mmEncoded").val(mes.mmEncoded);
					$("#mmUrl").val(mes.mmUrl);
					list = mes.children;
				}
			},'json');
		}
	}
</script>
</body>
</html>